package domain;

import java.io.Serializable;

import org.uqbar.commons.model.ObservableObject;
import domain.extra.FullTableException;
import domain.extra.InvalidChipsExeption;

public class Player extends ObservableObject implements Serializable{

	public static final String NAME = "name";
	public static final String MONEY = "money";
	public static final String CHIPS = "chips";
	public static final String JOINTHETABLE = "joinTheTable";
	public static final String LEAVETHETABLE = "leaveTheTable";
	public static final String JOIN = "join";

	private String name;
	private double money;
	private int chips;

	public Player(String name, double money) {
		this.name = name;
		this.money = money;
		this.chips = 0;
	}
	
	public Player(String name, int chips) {
		this.name = name;
		this.chips = chips;
	}


	public Player() {
		super();
	}

	/* ***************************************** */
	/* **************** ACTION ***************** */
	/* ***************************************** */

	public void makeBet(Bet bet, Table table) {
		table.addBet(bet);
		bet.setPlayer(this);
		this.chips -= bet.count; 
	}

	public void joinTheTable(Table table) {
		try {
			table.addPlayer(this);
		} catch (FullTableException fullTable) {
			System.out.println("La mesa esta llena");
		}
	}

	public void leaveTheTable(Table table) {
		table.removePlayer(this);
	}

	public void changeChips(){
		this.money -= this.chips;
	}
	
	public void changeChipsWithCount(int count) throws InvalidChipsExeption {
		if (isValidateCount(count)) {
//			this.chips += count;
//			this.money -= count;
			this.chips -= count; 
		} else {
			throw new InvalidChipsExeption("No es valida la cantidad de "
					+ "fichas que se desea cambiar");
		}
	}
	
	private boolean isValidateCount(int count) {
		return count > 0 && count < this.money;
	}

	/* ***************************************** */
	/* *************** ATRIBUTOS *************** */
	/* ***************************************** */

	@Override
	public String toString() {
		return "Player [name=" + name + ", money=" + money + ", chips=" + chips
				+ "]";
	}

	public void setName(String name) {
		this.setFieldValue(NAME, name);
	}


	
	public int getChips() {
		return chips;
	}

	public double getMoney() {
		return money;
	}

	public String getName() {
		return name;
	}

	public void setMoney(double money) {
		this.setFieldValue(MONEY, money);
	}

	public void setChips(int chips) {
		this.setFieldValue(CHIPS, chips);
	}

	public void addGain(int gain) {
		this.chips += gain;
	}
}
